package com.jilijili.mq;

import com.alibaba.fastjson.JSON;
import com.jilijili.config.mq.RabbitMQConfig;
import com.jilijili.enums.MessageEnum;
import com.jilijili.exceptions.GraceException;
import com.jilijili.grace.result.ResponseStatusEnum;
import com.jilijili.pojo.mo.MessageMo;
import com.jilijili.service.MsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @BelongsProject: jilijili-red-book-dev
 * @BelongsPackage: com.jilijili.mq
 * @Author: ljj
 * @CreateTime: 2022-04-12  10:27
 * @Description: TODO 消费者,监听队列
 * @Version: 1.0
 */
@Slf4j
@Component
public class RabbitMQConsumer {

    @Resource
    private MsgService msgService;


    @RabbitListener(queues = {RabbitMQConfig.QUEUE_SYS_MSG})
    public void waitQueue(String payload, Message message) {
        log.info("payload = " + payload);
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("routingKey = " + routingKey);

        if (routingKey.equalsIgnoreCase("routing.sys.msg." + MessageEnum.F0LLOW_Y0U.enValue)) {
            // TODO 关注通知
            MessageMo mo = JSON.parseObject(payload, MessageMo.class);
            msgService.createMsg(mo.getFromUserId(), mo.getToUserId(), MessageEnum.F0LLOW_Y0U.type, null);

        } else if (routingKey.equalsIgnoreCase("routing.sys.msg." + MessageEnum.UN_F0LLOW_Y0U.enValue)) {
            // TODO 取消关注
            MessageMo mo = JSON.parseObject(payload, MessageMo.class);
            this.msgService.delMessageByFromUserIdAndToUsersId(
                    mo.getFromUserId(),
                    mo.getToUserId());

        } else if (routingKey.equalsIgnoreCase("routing.sys.msg." + MessageEnum.COMMENT_Qg.enValue)) {
            // TODO 评论通知
            MessageMo messageMo = JSON.parseObject(payload, MessageMo.class);
            this.msgService.createMsg(
                    messageMo.getFromUserId(), // 评论用户
                    messageMo.getToUserId(), // 被评论的视屏用户
                    messageMo.getMsgType(), // 消息类型
                    messageMo.getMsgContent()); // 消息内容

        } else if (routingKey.equalsIgnoreCase("routing.sys.msg." + MessageEnum.REPLY_YOU.enValue)) {
            // TODO 回复评论通知
            MessageMo messageMo = JSON.parseObject(payload, MessageMo.class);
            this.msgService.createMsg(
                    messageMo.getFromUserId(), // 评论用户
                    messageMo.getToUserId(), // 被评论的视屏用户
                    messageMo.getMsgType(), // 消息类型
                    messageMo.getMsgContent()); // 消息内容

        } else if (routingKey.equalsIgnoreCase("routing.sys.msg." + MessageEnum.LIKE_VLOG.enValue)) {
            // TODO 点赞视频
            MessageMo messageMo = JSON.parseObject(payload, MessageMo.class);
            this.msgService.createMsg(
                    messageMo.getFromUserId(),
                    messageMo.getToUserId(),
                    messageMo.getMsgType(),
                    messageMo.getMsgContent());
        } else {
            // TODO 都不匹配抛出异常
            GraceException.display(ResponseStatusEnum.FAILED);
        }


    }


}
